package com.iqiyi.service.impl;

import com.iqiyi.mapper.VideoMapper;
import com.iqiyi.po.Video;
import com.iqiyi.po.VideoExample;
import com.iqiyi.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO
 *  电影的业务层
 * @author Administrator
 * @date 2021/2/24 11:30
 */
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {




    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 查询所有的影视信息
     * @return
     */
    @Override
    public List<Video> selectAllVideo() {
        redisTemplate.delete("videos");
//        Object videos = redisTemplate.opsForValue().get("videos");
////        if(videos != null){
////            log.info("get data from redis");
////            return (List<Video>)videos;
////        }
        VideoExample videoExample = new VideoExample();
        List<Video> all = videoMapper.selectByExample(videoExample);
        log.info("get data from db");
        redisTemplate.opsForValue().set("videos",all);
        return all;
    }

    /**
     * 根据影视分类查询符合条件的影视
     */
    @Override
    public List<Video> selectVideoBySort(Integer sid) {
        List<Video> videoSorts = videoMapper.selectVideoBySort(sid);
        return videoSorts;
    }

    @Override
    public Video selectByVid(Integer vid) {
        Video video = videoMapper.selectByPrimaryKey(vid);
        if (video!=null){
            return  video;
        }else {
            return null;
        }
    }

    @Override
    public boolean insertVideo(Video video) {
        int i = videoMapper.insertSelective(video);
        if (i>-1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Video> selectVideos() {

        List<Video> videos1= videoMapper.selectVideos();
        return videos1;
    }

    @Override
    public Video selectVideoByVid(Integer vid) {
        Video video = videoMapper.selectVideoByVid(vid);
        if (video!=null){
            return video;
        }else {
            return null;
        }

    }

    @Override
    public List<Video> selectSnameByLike(String sname) {
        List<Video> sname1 = videoMapper.selectSnameByLike(sname);
        if (sname1!=null){
            return sname1;
        }else {
            return null;
        }
    }

    /**
     * 通过视频id和剧集id查询视频url
     * @return
     */
    @Override
    public String selectVideoByVidAndEid(int vid, int enumber) {
        String videoUrl = videoMapper.findVideoByVidAndEnumber(vid,enumber);
        return videoUrl;
    }

    @Override
    public List<String> findVideoUrlByVid(int vid) {
        List<String> videoUrlByVid = videoMapper.findVideoUrlByVid(vid);
        return videoUrlByVid;
    }

    /**
     * 通过vid查询视频信息
     * @param vid
     * @return
     */
    @Override
    public Video findVideoByVid(int vid) {
        Video videoByVid = videoMapper.findVideoByVid(vid);
        return videoByVid;
    }
}
